package com.bookdemo.controller;

import com.alibaba.fastjson.JSON;
import com.bookdemo.pojo.Books;
import com.bookdemo.service.BookService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhu
 *
 */
@RestController
public class BookController {
    @Autowired
    BookService bookService;

    @RequestMapping("/books")
    public String getAllBooks(@RequestParam(defaultValue="1",required=true,value="pageNum") Integer pageNum) {
        Integer pageSize=3;//每页显示记录数
        //分页查询
        PageHelper.startPage(pageNum, pageSize);
        List<Books> books = bookService.getBooks();
        PageInfo<Books> pageInfo = new PageInfo<>(books);
        String s = JSON.toJSONString(pageInfo);
        return s;
    }

    @RequestMapping("/addBooks")
    public String addBooks(@RequestBody Books books) {
        int i = bookService.addBook(books);
        return i > 0 ? "success" : "error";
    }

    @RequestMapping("/deleteBook")
    public String deleteBook(int id) {
        int i = bookService.deleteBook(id);
        String str = i > 0 ? "success" : "error";
        return str;
    }

    @RequestMapping("/editBook")
    public String editBook(@RequestBody Books books) {
        int i = bookService.editBook(books);
        String str = i > 0 ? "success" : "error";
        return str;
    }
    @RequestMapping("/getUpadte")
    public String getUpdateBook(int id){
        System.out.println("编号"+id);
        Books updateBook = bookService.getUpdateBook(id);
        String s = JSON.toJSONString(updateBook);
        return s;
    }
    @RequestMapping("/selectBook")
    public String selectBook(String bookName){
        List<Books> books = bookService.selectBook(bookName);
        String s = JSON.toJSONString(books);
        return s;
    }



}
